package com.xyy.saas.payment.rpc;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xyy.ec.base.framework.rpc.ApiRPCResult;
import com.xyy.ec.order.backend.order.query.dto.OrderDto;
import com.xyy.ec.order.backend.out.paycenter.OrderForPaycenterApi;
import com.xyy.saas.payment.dao.model.PayFinanceRefundPlan;
import com.xyy.saas.payment.exception.PaymentException;
import com.xyy.saas.payment.pojo.convert.PayFinanceRefundPlanConvert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.curator.shaded.com.google.common.collect.Lists;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class EcOrderRemote {
    @Reference(version = "1.0.0")
    private OrderForPaycenterApi orderApi;

    public List<PayFinanceRefundPlan> queryListByPayNo(List<String> payNos) {
        if (CollectionUtils.isEmpty(payNos)) {
            return Lists.newArrayList();
        }
        try {
            ApiRPCResult<List<OrderDto>> listApiRPCResult = orderApi.queryListByPayNo(payNos);
            log.info("queryListByPayNo result:{}", listApiRPCResult.isSuccess());
            if (listApiRPCResult.isSuccess()) {
                return PayFinanceRefundPlanConvert.convertList(listApiRPCResult.getData());
            }
        } catch (Exception e) {
            log.error("queryListByPayNo error param:{}", payNos, e);
        }
        return Lists.newArrayList();
    }

    public List<OrderDto> queryOrderListByPayNo(List<String> payNos) {
        if (CollectionUtils.isEmpty(payNos)) {
            return Collections.emptyList();
        }
        try {
            ApiRPCResult<List<OrderDto>> result = orderApi.queryListByPayNo(payNos);
            log.info("queryOrderListByPayNo result:{}", result.isSuccess());
            if (result.isSuccess()) {
                return result.getData() == null ? Collections.emptyList(): result.getData();
            }
            throw new PaymentException(result.getErrMsg());
        } catch (Exception e) {
            log.error("queryOrderListByPayNo error param:{}", payNos, e);
            throw new RuntimeException(e instanceof PaymentException ? e.getMessage() : "查询订单信息异常");
        }
    }

        public Map<String, PayFinanceRefundPlan> queryListByPayNoMap(List<String> payNos) {
        if (CollectionUtils.isEmpty(payNos)) {
            return new HashMap<>();
        }
        Map<String, PayFinanceRefundPlan> res = new HashMap<>();
        try {
            ApiRPCResult<List<OrderDto>> listApiRPCResult = orderApi.queryListByPayNo(payNos);
            log.info("queryListByPayNo result:{}", listApiRPCResult.isSuccess());
            if (listApiRPCResult.isSuccess()) {
                for (OrderDto datum : listApiRPCResult.getData()) {
                    PayFinanceRefundPlan financeRefundPlan = PayFinanceRefundPlanConvert.convert(datum);
                    res.put(financeRefundPlan.getPayNo(), financeRefundPlan);
                }
            }
            return res;
        } catch (Exception e) {
            log.error("queryListByPayNo error param:{}", payNos, e);
        }
        return new HashMap<>();
    }

    public Map<String, PayFinanceRefundPlan> queryListByPayNoMapV2(List<String> payNos) {
        if (CollectionUtils.isEmpty(payNos)) {
            return new HashMap<>();
        }
        Map<String, PayFinanceRefundPlan> res = new HashMap<>();
        try {
            ApiRPCResult<List<OrderDto>> listApiRPCResult = orderApi.queryListByPayNoV2(payNos);
            //log.info("queryListByPayNoMapV2 result:{}", listApiRPCResult.isSuccess());
            if (listApiRPCResult.isSuccess()) {
                for (OrderDto datum : listApiRPCResult.getData()) {
                    PayFinanceRefundPlan financeRefundPlan = PayFinanceRefundPlanConvert.convert(datum);
                    res.put(financeRefundPlan.getPayNo(), financeRefundPlan);
                }
            }

            return res;
        } catch (Exception e) {
            log.error("queryListByPayNoMapV2 error param:{}", payNos, e);
        }
        return new HashMap<>();
    }

}
